﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GodPl.GameObjects;
using GodPl.WorldState;
using GodPl.GameObjects.Creatures;

namespace GodPl.GameObjects.Creatures.Behaviors
{
    public class StandardBriyyaBehavior : IBriyyaUpdater
    {
        #region Scaffolding
        private Briyya actor;
        public Briyya Actor
        {
            get { return actor; }
        }


        public Briyya Me
        {
            get { return actor; }
        }

        public World World
        {
            get { return actor.World; }
        }

        public void print(string str)
        {
            GodPlGame.LogLine(str);
        }

        public void print(Object o)
        {
            GodPlGame.LogLine(o.ToString());
        }
        #endregion

        #region Static Variables
        #endregion

        #region Instance Variables
        IFoodSource myFoodSource;
        Briyya myPartner;

        int interval1 = GodPlGame.RandomGen.Next(7, 13);
        int interval2 = GodPlGame.RandomGen.Next(2, 4);
        int interval3 = GodPlGame.RandomGen.Next(12, 16);

        #endregion



        #region IBriyyaUpdater Members

        public void SetActor(Briyya actor)
        {
            this.actor = actor;
        }
    
        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            if (GodPlGame.Every(0, 0, interval1) &&
                Me.Drives[DriveName.Hunger] > Me.Drives[DriveName.SexDrive])
            {
                //myFoodSource = GodPlGame.PickRandom<IFoodSource>();
                //Me.MoveTowards((GameObject)myFoodSource);
            }

            if (GodPlGame.Every(0, 0, interval2))
            {
                DriveName dominant = Me.Drives.PickDominantDrive();

                if (dominant == DriveName.Tiredness)
                {
                    Me.Sleep();
                }
                else if (dominant == DriveName.SexDrive &&
                         Me.LifeStage == LifeStage.Adult &&
                         Me.Drives[DriveName.SexDrive] >= 0.75f)
                {
                    breed();
                }
                else if (dominant == DriveName.Hunger)
                {
                    if (eatFromNearby() && GodPlGame.Roll(0.75f))
                    {

                    }
                    else
                    {
                        var myFoodSource = GodPlGame.PickRandom<IFoodSource>();
                        Me.MoveTowards((GameObject)myFoodSource);
                    }
                }
                else
                {
                    var target = GodPlGame.PickRandom<IFoodSource>();
                    Me.MoveTowards((GameObject)target);
                }
            }

        }

        private bool eatFromNearby()
        {
            var foodsources = from s in this.World.GameObjects
                              where ((s as IFoodSource) != null &&
                                     Me.IsAdjacentTo(s))
                              select s as IFoodSource;

            if (foodsources.Count() > 0)
            {
                Me.EatFrom(foodsources.ElementAt(0));
                return true;
            }

            return false;
        }

        #endregion

        void breed()
        {
            var partners = from o in this.World.Briyyas
                           where (o.Sex != Me.Sex &&
                                  o.LifeStage == LifeStage.Adult)
                           select o as Briyya;

            if (partners.Count() == 0)
            {
                return;
            }

            myPartner = GodPlGame.PickRandom(partners);
            Me.BreedWith(myPartner);
            
        }


    }
}
